package com.github.housepower.jdbc.benchmark;

import com.google.common.base.Strings;

import org.junit.Assert;
import org.junit.Test;
import org.openjdk.jmh.annotations.Benchmark;

import java.sql.PreparedStatement;

import ru.yandex.clickhouse.ClickHouseStatement;
import ru.yandex.clickhouse.domain.ClickHouseFormat;

/**
 */
public class RowBinaryDoubleIBenchmark extends AbstractInsertIBenchmark {
    private String columnType = "Float64";

    @Benchmark
    @Test
    public void benchInsertNative() throws Exception {
        withConnection(connection -> {
            wideColumnPrepare(connection, columnType);
            String params = Strings.repeat("?, ", columnNum);
            PreparedStatement
                pstmt = connection.prepareStatement("INSERT INTO " + getTableName() + " values(" + params.substring(0, params.length() - 2) + ")");
            for (int i = 0; i < batchSize; i++) {
                for (int j = 0; j < columnNum; j++ ) {
                    pstmt.setDouble(j + 1, j + 1.0);
                }
                pstmt.addBatch();
            }
            int []res = pstmt.executeBatch();
            Assert.assertEquals(res.length, batchSize);
            wideColumnAfter(connection);

        }, ConnectionType.NATIVE);
    }

    @Benchmark
    @Test
    public void benchInsertHttpRowBinary() throws Exception {
        withConnection(connection -> {
            wideColumnPrepare(connection, columnType);
            ClickHouseStatement sth = (ClickHouseStatement) connection.createStatement();
            sth.write().send("INSERT INTO " + getTableName(), stream -> {
                for (int i = 0; i < batchSize; i++) {
                    for (int j = 0; j < columnNum; j++ ) {
                        stream.writeFloat64(j + 1.0);
                    }
                }
            }, ClickHouseFormat.RowBinary);

            wideColumnAfter(connection);
        }, ConnectionType.HTTP);
    }

}
